/**
 * 面板
 * @author wonderful wuketidai@foxmail.com
 * @create 20150626
 */
(function(){
	/*	var opt={
	 isDrag:true,//如果为true就可拖拽
	 width:400,//高
	 haveUpload:true,//是否有上传文件功能
	 uploadParam:{	//上传模块参数，若haveUpload为true必传
	 uploadUrl:""	//上传文件URL
	 },
	 height:500,//宽
	 valForm:{
	 valParam:[//验证参数
	 {
	 inputname:"loginName",
	 isnull:true,
	 isnullmsg:"登录名不能为空！",
	 exists:true,
	 existsmsg:"用户名已被使用！",
	 postdata:function(){
	 return "loginName="+$("#loginName").val();
	 },
	 url:basePath+"gzmli/system/isLoginNameExists.wdf",
	 istrue:1,
	 successfulMsg:"用户名输入正确",
	 showMsg:"",
	 event:"blur"
	 },
	 {
	 inputname:"userName",
	 isnull:true,
	 isnullmsg:"用户姓名不能为空",
	 event:"blur"
	 }
	 ]
	 },//验证
	 action:"",//表单提交地址
	 gridOption : tunnelGridOpt,//wdfGrid参数对象
	 treeOption:treeOption,//zTree参数
	 treeDataRoot:"",//zTree数据的根属性
	 event:{
	 ztreeDataFilter:function(zTreeObj,nodes){//zTree非异步下的数据过滤事件

	 },
	 okBtnClick:function(eventObj,panel,{//该参数为表单模式下启用，通过在函数里设success的值来控制表单提交
	 //0不提交，继续等待 1提交 2 退出
	 success:null,
	 msg:"",
	 }){

	 },
	 resetBtnClick:function(eventObj,panel){

	 },onClosed:function(obj){

	 },
	 submitSuccessed:function(data,panelObj){//提交成功后执行data提交后服务端返回的数据，必须包含执行状态state(0/1),panelObj当前面板对象

	 },onShowed:function(panelObj){//面板显示的时候触发

	 }
	 },
	 extendsBtn:[//扩展按钮,GridPanel模式下有效
	 {
	 label:"",//按钮名称
	 id:"userTunnelBind",//按钮id
	 className:"filterBtn",//按钮样式名
	 clickEvent:function(eventObj,panel,grid){

	 }
	 }]
	 };

	 function showMsg(msg){
	 var msgBar=$("<div class='msgBar'>"+msg+"</div>");
	 $("#addUserPanel").append(msgBar);
	 msgBar.animate({"opacity":1},500);
	 };*/


    $.fn.extend({"wdfPanel":function(type,opt){

        var obj=this;
        obj.css({width:opt.width,height:opt.height});
        var titleBar=this.find(".titleBar");
        var splitBar=this.find(".split");
        var closeBtn=this.find(".closeBtn");
        var content=this.find(".content");
        var btnBar=this.find(".btnBar");
        var okBtn=btnBar.find(".okBtn");
        var resetBtn=btnBar.find(".resetBtn");
        var quxiao_btn = obj.find(".quxiao_btn");
        var searchBar;
        var wdfGrid;
        var actionBar;
        //content.html("");
        var pannel_bg = this.parent(".pannel_bg");
        pannel_bg.css("height","100%");

        function showMsg(msg){
            var msgBar=$("<div  class='msgBar'>"+msg+"</div>");
            $(".msgBar").remove();
            obj.append(msgBar);
            msgBar.animate({"opacity":1},500);
        }
        function showBg(content){
            var bg=$("<div class='bg'></div>").appendTo(content);
            var bg_LoadProgress=$("<div class='bg_LoadProgress'></div>").appendTo(content);
        }
        function removeBg(content){
            content.find(".bg").remove();
            content.find(".bg_LoadProgress").remove();
        };

        var btnBarHeight=obj.find(".btnBar").height();
        if(opt.isDrag){//可拖拽
            obj.find(".titleBar").css("cursor","move");
            obj.find(".titleBar").dragDiv(".panel");

        }
        if(type=="GridPanel"){
            content.height(opt.height-60);
        }else if(type=="TreePanel"){

            content.height(opt.height-166);
        }else{
            //	content.height(opt.height-btnBarHeight);
        }


        if(opt.bombType == 1){
            closeBtn.hide();
            titleBar.addClass("bombType1_title");
            content.addClass("bombType1_content");
        }else if(opt.bombType == 2){
            closeBtn.hide();
            titleBar.addClass("bombType2_title");
            content.addClass("bombType2_content");
        }else if(opt.bombType == 3){
            titleBar.addClass("bombType3_title")
        }else if(opt.bombType == 4){
            titleBar.addClass("bombType3_title");
            content.addClass("bombType2_content");
        }

        quxiao_btn.bind("click",function () {
            if(opt.event&&opt.event.onClosed)
            {
                opt.event.onClosed(obj);
            }
            var resetBtn=obj.find(".resetBtn");
            if(resetBtn.length>0){
                resetBtn.data("isClose",true);

                resetBtn.click();
            }
            if(obj.find('#uploadfile').size()>0){

            }
            obj.css("display","none");
            obj.parent(".pannel_bg").hide();
            $(".tempFileList").remove();
        })
        closeBtn.bind("click",function(){

            if(opt.event&&opt.event.onClosed)
            {
                if(opt.event.onClosed(obj)==false)
                    return;
            }
            var resetBtn=obj.find(".resetBtn");
            if(resetBtn.length>0){
                resetBtn.data("isClose",true);

                resetBtn.click();
            }
            if(obj.find('#uploadfile').size()>0){

            }
            obj.css("display","none");
            obj.parent(".pannel_bg").hide();
            $(".tempFileList").remove();
            $("div[id*=_wdfGridEditRowSel_]").remove();
        });

        //如果是表单面板
        if(type=="FormPanel"){

            if(opt.valForm&&opt.valForm.valParam){
                createValidateEvent(function(){
                    return opt.valForm.valParam;
                });
            }
            if(opt.action||opt.action==""){
                okBtn.bind("click",function(){
                    if(opt.event["okBtnClick"]){
                        //标识，okBtnClick后判断表单是否提交
                        var identifying={
                            success:null,
                            msg:"",
                        }
                        opt.event["okBtnClick"]($(this),obj,identifying);
                        setTimeout(intervalRef,100);
                        //间隔判断标识状态
                        function intervalRef(){
                            if(identifying.success==0){
                                setTimeout(intervalRef,100);
                            }else if(identifying.success==1){
                                submitForm();
                            }else if(identifying.success==null){
                                submitForm();
                            }else{
                                return;
                            }
                        }
                        return;
                    }
                    //直接提交
                    submitForm();
                    //表单提交
                    function submitForm(){
                        if(opt.valForm&&opt.valForm.valParam&&!executeVal(opt.valForm.valParam)){
                            return;
                        }
                        $.wdfProgress.progress("请稍等...",obj,"center");
                        setTimeout(function(){
                            var param=obj.find("form").serialize();
                            // param=encodeURI(param);
                            //alert(param);
                            $.ajax({
                                url:opt.action,
                                //processData: false,
                                //data: xmlDocument,
                                data:param,
                                dataType:"json",
                                type: "post",
                                contentType:"application/x-www-form-urlencoded;",
                                beforeSend:null,
                                success:function(data){
                                    if(data&&(data.status==1 || data.state==1)){
                                        if(data&&data.msg)
                                            $.wdfProgress.showMsg(data.msg,obj,false);
                                    }else{
                                        $.wdfProgress.showMsg(data.msg ,obj,false);
                                        return;
                                    }
                                    //提交成功后执行
                                    if(opt.event.submitSuccessed){
                                        opt.event.submitSuccessed(data,obj);
                                    }
                                },
                                error:function (XMLHttpRequest, textStatus, errorThrown) {
                                    $.wdfProgress.showMsg("网络延迟或系统异常！",obj,false);
                                }});

                        },300);
                    }
                });}
            obj.css("display","none");
            obj.parent(".pannel_bg").hide();
			/*obj.find("div:.content").bind("click",function(e){
			 stopBubble(e);
			 });
			 obj.find("div:.content").bind("mousedown",function(e){
			 stopBubble(e);
			 });*/
        }//表单面板end

        else if(type=="GridPanel"){
            searchBar=createSearchBar(content,opt);
            wdfGrid=createWdfGrid(content,opt);
            actionBar=createActionBar(content,opt,wdfGrid,searchBar);
            formBar = createFormBar(content,opt);
            obj.css("display","none");
            obj.parent(".pannel_bg").hide();
        }
        else if(type=="TreePanel"){
            var treeObj=$("<ul id='"+opt.id+"_ZTree' class='ztree'></ul>").appendTo(content);
            $.wdfProgress.progress("请稍等...",obj);
            $.ajax({
                url:opt.action,
                dataType:"json",
                type: "post",
                contentType:"application/x-www-form-urlencoded",
                beforeSend:null,
                data:opt.param,
                success:function(data){
                    if(opt.event&&opt.event.ztreeDataFilter){
                        if(data.nodes){
                            opt.event.ztreeDataFilter(data.nodes);
                            $.fn.zTree.init(treeObj,opt.ztreeOption,data.nodes)
                        }else if(opt.treeDataRoot){
                            opt.event.ztreeDataFilter(data[opt.treeDataRoot]);
                            $.fn.zTree.init(treeObj,opt.ztreeOption,data[opt.treeDataRoot]);
                        }

                    }
                    else{
                        if(data.nodes){
                            $.fn.zTree.init(treeObj,opt.ztreeOption,data.nodes);
                        }else if(opt.treeDataRoot){
                            $.fn.zTree.init(treeObj,opt.ztreeOption,data[opt.treeDataRoot]);
                        }
                    }

                    $.wdfProgress.showMsg("数据加载完成！",obj);
                    $.wdfProgress.progRemove(obj);
                },
                error:function (XMLHttpRequest,textStatus,errorThrown) {
                    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),false);
                }});

            if(opt.event&&opt.event.okBtnClick&&okBtn.length>0){
                okBtn.bind("click",function(){
                    opt.event.okBtnClick($(this),obj);
                });
            }

            if(opt.event&&opt.event.resetBtnClick&&resetBtn.length>0){
                resetBtn.bind("click",function(){
                    opt.event.resetBtnClick($(this),obj);
                });
            }

            obj.css("display","none");
            obj.parent(".pannel_bg").hide();
        }else if(type=="Custom"){


        }
        obj.show=function(title,gridInit){
            obj.find(".titleBar").find(".title").text(title);
            obj.css("display","block");
            pannel_bg.css("height","100%");
            pannel_bg.show();
            var uploader;
            if(opt.haveUpload){
                obj.find("#"+obj.attr("id")+"uploader").remove();

                var uploadfile = $('<div id="'+obj.attr("id")+'uploader" class="uploader_box"></div>').appendTo(content);


                var form_group = $('<div class="form-group form-inline"></div>').appendTo(uploadfile);
                var picker = $('<div id="'+obj.attr("id")+'picker">选择文件</div>').appendTo(form_group);
                var $thelist = $('<div id="'+obj.attr("id")+'thelist" class="uploader-list"></div>').appendTo(uploadfile);

                var btnBar = $('<div class="btnBar"></div>');
                btnBar.appendTo(uploadfile);

                var ctlBtn = $('<input type="button" value="开始上传" id="'+obj.attr("id")+'ctlBtn" class="zhuanjiao_btn"/>').appendTo(btnBar);
                var uploadCloseBtn = $('<input value="取消" class="quxiao_btn" type="button">').appendTo(btnBar);
                var $upbtn = obj.find('#'+obj.attr("id")+'ctlBtn');
                uploadCloseBtn.bind("click",function () {
                    obj.find(".closeBtn").click();
                });
                uploader = WebUploader.create({
                    //auto:true,
                    fileNumLimit: 10,//上传数量限制
                    resize: false, // 不压缩image
                    swf: basePath+'resource/plugins/webuploader/js/uploader.swf', // swf文件路径
                    // server: basePath+'file/wdfUpload.wdf?statusName=111&projectDocType=1', // 文件接收服务端。
                    server:opt.uploadParam.uploadUrl,
                    pick: {id:'#'+obj.attr("id")+'picker',
                        style:{
                            color:'#000'
                        }
                    }, // 选择文件的按钮。可选
                    chunked: false, //是否要分片处理大文件上传
                    //  chunkSize:2*1024*1024 //分片上传，每片2M，默认是5M
                    //auto: false //选择文件后是否自动上传
                    // chunkRetry : 2, //如果某个分片由于网络问题出错，允许自动重传次数
                    //runtimeOrder: 'html5,flash',
                    // accept: {
                    //   title: 'Images',
                    //   extensions: 'gif,jpg,jpeg,bmp,png',
                    //   mimeTypes: 'image/*'
                    // }
                });
                uploader.on( 'fileQueued', function( file ) {
                    var size = file.size;
                    if(size>1024){
                        size = (size/1024) .toFixed(2)+"kb";
                    }else if(size>1024*1024){
                        size = (size/(1024*1024)) .toFixed(2)+"mb";
                    }else{
                        size = size + "b";
                    }
                    var file_ext = file.ext;
                    var file_type_class = '';
                    if(file_ext == "jpg" || file_ext =="JPG"){
                        file_type_class = "jpg";
                    }else if(file_ext == "png" || file_ext =="PNG"){
                        file_type_class = "png";
                    }else if(file_ext == "docx" || file_ext =="doc"){
                        file_type_class = "doc";
                    }else if(file_ext == "xls" || file_ext =="xlsx"){
                        file_type_class = "xlsx";
                    }else if(file_ext == "ppt" || file_ext =="pptx"){
                        file_type_class = "ppt";
                    }else if(file_ext == "txt" || file_ext =="TXT"){
                        file_type_class = "txt";
                    }
                    $thelist.append( '<div id="' + file.id + '" class="file_item '+file_type_class+'">' +
                        '<div class="upload_list_left"><p class="info">' + file.name + '</p>' +

                        '<p class="state">文件大小：'+size+'</p></div>' +'<span class="ion-close-round">删除</span>'+ '</div>' );
                    obj.find(".webuploader-pick").css("height","48px");
                    opt.event.onUploadFileQueued(file);
                });
                uploader.on( 'uploadProgress', function( file, percentage ) {
                    var $li = $( '#'+file.id ).find(".upload_list_left");
                    var $percent = $li.find('.progress-bar');

                    $li.find(".progress").remove();
                    // 避免重复创建
                    //  $li.find('.progress-bar').html("");
                    $percent = $('<div class="progress progress-striped active">' +
                        '<div class="progress-bar" role="progressbar" style="width:0%">' +
                        '</div>' +
                        '</div>').appendTo( $li );
                    $percent.find(".progress-bar").css( 'width', percentage * 100 + '%' );
                    $( '#'+file.id ).find('.state').hide();
                });
                uploader.on('beforeFileQueued',function (file) {

                });
                // 文件上传成功
                uploader.on( 'uploadSuccess', function( file ,data) {
                    delete data._raw;
                    file.type=file.ext;
                    $( '#'+file.id ).find('.state').text('已上传');
                    opt.event.onUploadSuccessed(file,data);
                });

                // 文件上传失败，显示上传出错
                uploader.on( 'uploadError', function( file ) {
                    $( '#'+file.id ).find('p.state').text('上传出错');
                });
                // 完成上传完
                uploader.on( 'uploadComplete', function( file ) {

                });
                uploader.on('uploadFinished',function () {//上传结束
                    opt.event.uploadComplete();
                });
                $upbtn.on('click', function () {
                    if(uploadfile.find(".file_item").size()>10){
                        $.wdfProgress.progress("文件数量不能大于10！", $("body"),true,false);
                        $.wdfProgress.progRemove($("body"),false,1000);
                        return false;
                    }

                    if ($(this).hasClass('disabled')) {
                        return false;
                    }

                    uploader.upload();

                });
                $thelist.on("click", ".ion-close-round", function () {
                    var fileItem = $(this).parent();
                    var id = $(fileItem).attr("id");
                    uploader.removeFile(id, true);
                    $(fileItem).fadeOut(function () {
                        $(fileItem).remove();
                    });

                });




                obj.find('.webuploader-pick').next("div").css("width","100%");
                obj.find('.webuploader-pick').next("div").css("height","100%");
            }
            var btnBarHeight=obj.find(".btnBar").height();
            var titleBarHeight=obj.find(".titleBar").height();

            //content.height(opt.height-splitBar[0].offsetHeight*2-titleBar[0].offsetHeight-btnBarHeight-10);

            if(!gridInit){
                //	content.height(opt.height-btnBarHeight-titleBarHeight-30);
            }

            obj.css({"margin-top":opt.height/2*-1,"margin-left":obj[0].offsetWidth/2*-1});
            obj.css({"left":"50%","top":"50%"});
            if(gridInit){
                opt.gridOption.param = searchBar.find("form").serialize();
                wdfGrid.wdfGrid("init",opt.gridOption);
            }
            if(opt.event&&opt.event["onShowed"]){
                opt.event.onShowed(obj);
            }
            if(opt.formOption){

            }
        };
        obj.close=function(title,gridInit){
            obj.css("display","none");
            obj.parent(".pannel_bg").hide();
        };

        return obj;

    }});


    function createWdfGrid(contentObj,opt){

        var wdfGrid=$("<div id='"+opt.id+"_WdfGrid' style='background-color:white'></div>");
        var gridContent=$("<div></div>");
        contentObj.append(gridContent);
        gridContent.append(wdfGrid);
        return wdfGrid;

    }

    function createSearchBar(contentObj,opt){
        var searchBox = $("<div class='searchBar_box'></div>");
        var  searchBar_Blue= $("<div class='searchBar_Blue' ></div>");

        var searchBar=$("<div class='wdfSearchBar'></div>");
        contentObj.append(searchBar);
        var form=$("<form id='"+opt.id+"_sonPage_searchForm'></form>");
        searchBox.append(searchBar_Blue);
        form.append(searchBox);
        searchBar.append(form);
        if(opt.searchInput)
        {
            $(opt.searchInput).each(function(index){
                var input;
                if(this.type=="hidden")
                {
                    var input=$("<input/>");
                    input.attr("id",this.id);
                    input.attr("type",this.type);
                    input.attr("name",this.name);
                    if(this.val)
                        input.val(this.val);
                    if(this.isConst)
                        input.attr("isConst",'true');
                    form.append(input);
                    return;
                }


                var block=$("<div class='block'></div>");
                var label=$("<div class='label'>"+this.title+"：</div>");
                var inputDiv = $("<div class='input1 input'></div>")

                block.append(label);

                if(this.type=="select"){
                    input=$("<select></select>");
                    input.attr("id",this.id);
                    input.attr("name",this.name);
                }else{
                    input=$("<input/>");
                    input.attr("id",this.id);
                    input.attr("type",this.type);
                    input.attr("name",this.name);
                };
                if(this.val)
                    input.val(this.val);
                if(this.isConst)
                    input.attr("isConst",'true');

                inputDiv.append(input);
                block.append(inputDiv);

                searchBar_Blue.append(block);
            });
            //重置按钮
            var hasInputText = 0;

            $(opt.searchInput).each(function(index){
                if(this.type=="text"||this.type=="select"){
                    hasInputText = 1;
                }
            })
            if(hasInputText == 1)
            {
                var resetBtn=$("<div class='optContent'><input type='button' class='input_button research_btn' id='"+opt.id+"_sonPage_resetBtn'  value='重置' </div>");
                searchBar_Blue.append(resetBtn);
            }
        }

        //回车绑定事件
        $("input[type=text]").keyup(function(e){
            var theEvent = window.event || e;
            if(theEvent.keyCode ==13){
                //alert("test");
                $("#"+opt.id+"_sonPage_btn").click();

            }
        });
        $("select").change(function () {
            $("#"+opt.id+"_sonPage_btn").click();
        })

        return searchBar;
    }


    function createActionBar(contentObj,opt,wdfGrid,searchBar){
        var actionBar=$("<div class='actionBar'></div>");

        if(opt.searchInput&&opt.searchInput.length>0)
        {

            //重置按钮点击事件
            $("#"+opt.id+"_sonPage_resetBtn").bind("click",function(){
                $('#'+opt.id+'_sonPage_searchForm')[0].reset();
                opt.gridOption.param = $("#"+opt.id+"_sonPage_searchForm").serialize();
                wdfGrid.wdfGrid("reload",opt.gridOption);
            });


        }





        //actionBar.width(contentObj.width());

		/*	if(opt.searchInput&&opt.searchInput.length>0)
		 {
		 var searchBtn=$("<input type='button' class='searchBtn' value='搜索'/>");
		 actionBar.append(searchBtn);
		 searchBtn.bind("click",function(){
		 opt.gridOption.param = searchBar.find("form").serialize();
		 if(opt.isSearchDebug)
		 $("[id*='_replay']").remove();
		 wdfGrid.wdfGrid("reload",opt.gridOption);

		 });
		 }

		 if(opt.searchInput&&opt.searchInput.length>0){
		 var resetBtn=$("<input type='button' class='resetBtn' value='重置'/>");
		 actionBar.append(resetBtn);
		 resetBtn.bind("click",function(){

		 searchBar.find("input").not("input[isConst='true']").val("");
		 if($(this).data("isClose"))
		 {
		 $(this).data("isClose",false);
		 return;
		 }
		 opt.gridOption.param = searchBar.find("form").serialize();
		 $("[id*='_replay']").remove();
		 wdfGrid.wdfGrid("reload",opt.gridOption);

		 });
		 }
		 */
        if(opt.inputBtn){

            $(opt.inputBtn).each(function(i,inputBtn){
                //通过自定义按钮类型添加不同的样式
                var customerBtn;
                if(inputBtn.btnType == "add"){
                    customerBtn=$('<input type="button" class="input_button myAddBtn" id="'+inputBtn.id+'" value="'+inputBtn.title+'"/>');
                }else if(inputBtn.btnType == "upload"){
                    customerBtn=$('<input type="button" class="input_button myUploadBtn" id="'+inputBtn.id+'" value="'+inputBtn.title+'"/>');
                }else{
                    customerBtn=$('<input type="button" class="input_button" id="'+inputBtn.id+'" value="'+inputBtn.title+'"/>');
                }

                if(inputBtn.onClick){
                    customerBtn.bind("click",function(){
                        inputBtn.onClick($(this),contentObj,wdfGrid);
                    });
                }

                actionBar.append(customerBtn);
            });

        }


		/*var closeBtn=$("<a class='closeBtn'></a>");
		 actionBar.append(closeBtn);
		 closeBtn.bind("click",function(){
		 contentObj.animate({opacity:0},500);
		 setTimeout(function(){
		 contentObj.css("display","none");
		 if(opt.onClosed)
		 {
		 opt.onClosed();
		 }
		 },500);
		 $("[id*='_replay']").remove();
		 });*/

        if(opt.extendsBtn)
        {
            $(opt.extendsBtn).each(function(i,btnParam){
                var btn=$("<a></a>");
                btn.attr("id",btnParam.id);
                btn.attr("class",btnParam.className);
                btn.text(btnParam.label);
                actionBar.append(btn);
                if(btnParam.clickEvent){
                    btn.bind("click",function(){
                        btnParam.clickEvent($(this),contentObj,wdfGrid);
                    });
                }
            });
        }
        wdfGrid.parent().before(actionBar);
        contentObj.css("display","block");
        //动态计算表格控件高度
        // opt.gridOption.height=opt.height-98;
        // wdfGrid.wdfGrid("init",opt.gridOption);
        // contentObj.css("display","none");
        if(searchBar.find("input[type=text]").length<1){
            actionBar.find(".searchBtn").hide();
            actionBar.find(".resetBtn").hide();
        }
        return actionBar;
    }

    function createFormBar(contentObj,opt) {
        var obj = contentObj.parent();
        if(opt.formOption){
            var formBar=$("<div class='wdfformBar'></div>");
            contentObj.append(formBar);
            var form=$("<form></form>");
            formBar.append(form);
            $(opt.formOption.formInput).each(function(index){
                var input;
                if(this.type=="hidden")
                {
                    var input=$("<input/>");
                    input.attr("id",this.id);
                    input.attr("type",this.type);
                    input.attr("name",this.name);
                    if(this.val)
                        input.val(this.val);
                    if(this.isConst)
                        input.attr("isConst",'true');
                    form.append(input);
                    return;
                }
                var col=$("<div class='col'></div>");
                if(this.type == "checkbox"||this.type == "radio"){
                    var title=$("<div class='title'>"+this.title+"</div>");
                }else{
                    var title=$("<div class='title'>"+this.title+"："+"</div>");
                }

                if(this.type !="checkbox" && this.type !="radio"){
                    col.append(title);
                }

                if(this.type=="select"){
                    input=$("<select></select>");
                    input.attr("id",this.id);
                    input.attr("name",this.name);
                }else if(this.type=="textarea"){
                    input=$("<textarea></textarea>");
                    input.attr("id",this.id);
                    input.attr("name",this.name);
                }
                else{
                    input=$("<input/>");
                    input.attr("id",this.id);
                    input.attr("type",this.type);
                    input.attr("name",this.name);
                }
                if(this.val)
                    input.val(this.val);
                if(this.isConst)
                    input.attr("isConst",'true');
                if(this.isClear){
                    col.css("clear","both")
                }

                col.append(input);
                if(this.type =="checkbox" || this.type =="radio"){
                    title.css("paddingLeft",0);
                    title.css("text-align","left");
                    col.append(title);
                }
                form.append(col);
                form.attr("action",opt.formOption.action);
            });
            contentObj.parent().find("#doSubmit").click(function () {
                if(opt.formOption.beforeSubmit(this)){
                    var canSub = true;
                    $(opt.formOption.formInput).each(function(index){
                        if(this.isnull == true && contentObj.find("#"+this.id).val()==""){
                            contentObj.find("#"+this.id).css("borderColor","#ff0000");
                            contentObj.find("#"+this.id).css("backgroundColor","#f5a5a5");
                            canSub = false;
                        }else{
                            contentObj.find("#"+this.id).css("borderColor","#dadada");
                            contentObj.find("#"+this.id).css("backgroundColor","none");
                        }
                    });
                    if(canSub){
                        $.wdfProgress.progress("请稍等...",contentObj.parent(),"center");
                        var param=form.serialize();
                        $.ajax({
                            url:basePath+"gzmli/project/listUserRoles",
                            //processData: false,
                            //data: xmlDocument,
                            data:param,
                            dataType:"json",
                            type: "post",
                            contentType:"application/x-www-form-urlencoded",
                            beforeSend:null,
                            success:function(data){
                                if(data&&(data.status==1 || data.state==1)){
                                    if(data&&data.msg)
                                        $.wdfProgress.showMsg(data.msg,obj,false);
                                }else{
                                    $.wdfProgress.showMsg("操作失败！",obj,false);
                                    return;
                                }
                                //提交成功后执行
                                if(opt.event.submitSuccessed){
                                    opt.event.submitSuccessed(data,obj);
                                }
                            },
                            error:function (XMLHttpRequest, textStatus, errorThrown) {
                                $.wdfProgress.showMsg("网络延迟或系统异常！",obj,false);
                            }});
                    }


                }
            })
        }



        return formBar;

    }

})();
(function($) {
    $.fn.dragDiv = function(divWrap) {
        return this.each(function() {
            var $divMove = $(this);//鼠标可拖拽区域
            var $divWrap = divWrap ? $divMove.parents(divWrap) : $divMove;//整个移动区域
            var mX = 0, mY = 0;//定义鼠标X轴Y轴
            var dX = 0, dY = 0;//定义div左、上位置
            var isDown = false;//mousedown标记

            var $removeDiv = $divMove.find(".remove_btn");

            if(document.attachEvent) {//ie的事件监听，拖拽div时禁止选中内容，firefox与chrome已在css中设置过-moz-user-select: none; -webkit-user-select: none;
                $divMove[0].attachEvent('onselectstart', function() {
                    return false;
                });
            }
            $divMove.mousedown(function(event) {
                var event = event || window.event;

                mX = event.clientX;
                mY = event.clientY;
                dX = $divWrap.offset().left;
                dY = $divWrap.offset().top;
                var x = event.clientX;//鼠标滑动时的X轴
                var y = event.clientY;//鼠标滑动时的Y轴
                $divWrap.css({"left": x - mX + dX, "top": y - mY + dY});//div动态位置赋值
                $divWrap.css("marginLeft","0");
                $divWrap.css("marginTop","0");
                isDown = true;//鼠标拖拽启动
            });
            $(document).mousemove(function(event) {
                var event = event || window.event;
                var x = event.clientX;//鼠标滑动时的X轴
                var y = event.clientY;//鼠标滑动时的Y轴
                if(isDown) {
                    $divWrap.css({"left": x - mX + dX, "top": y - mY + dY});//div动态位置赋值
                }
            });
            $divMove.mouseup(function() {
                isDown = false;//鼠标拖拽结束
            });
            $removeDiv.click(function () {
                $divWrap.hide(300);
            });
        });
    };
})(jQuery);



